package leetcode.DFS_BFS;

/**
 * @author mazouri
 * @create 2021-09-22 23:46
 */
public class _695_maxAreaOfIsland {
    int maxArea = 0;
    int[][] grid;
    int[] dX = {0, 1, 0, -1};
    int[] dY = {1, 0, -1, 0};

    public int maxAreaOfIsland(int[][] grid) {
        this.grid = grid;

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (this.grid[i][j] == 1) {
                    maxArea = Math.max(maxArea, dfs(i, j));
                }
            }
        }
        return maxArea;
    }

    private int dfs(int x, int y) {
        if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length) return 0;
        if (grid[x][y] != 1) return 0;

        grid[x][y] = 0;
        int res = 1;
        for (int i = 0; i < 4; i++) {
            res += dfs(x + dX[i], y + dY[i]);
        }
        return res;
    }
}
